<?php

namespace  App\Http\Controllers\V1;

use App\Helpers\Logger\SLogger;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Http\Controllers\Controller;
use App\Models\Factory\CoinFactory;
use App\Models\Factory\SmsFactory;
use App\Models\Factory\UserFactory;
use App\Models\Orm\Banner;
use App\Models\Orm\CoinDrawLog;
use App\Models\Orm\CoinMarket;
use App\Models\Orm\CoinStoreLog;
use App\Models\Orm\UserCoinLog;
use App\Services\Core\Sms\Dahan\DahanService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class GeetesController
 * @package App\Http\Controllers\V1
 * 短信
 */
class CoinController extends  Controller
{

    /**
     * 发送短信
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCoin(Request $request)
    {

        $data = CoinMarket::select('percent_change_24h','price')->where('name','Ethereum')->first();
        $data['price'] = round($data['price'],2);
        return RestResponseFactory::ok($data,200);

    }



    //存币
    public function storeCoin(Request $request)
    {
        //存币
        $data = $request->all();
        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        //首先查询是否有未来处理的订单
        $hasCoin = CoinFactory::hasCoinStoreLog($user->id);

        if($hasCoin == true)
        {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 30002, 30002, RestUtils::getErrorMessage(30002));;

        }

//        DB::beginTransaction();
//        try {
            //插入存币日志
            $total_bi = $user->total_bi;
            $CoinId = CoinFactory::addCoinStoreLog($user->id,$data,$total_bi);

            if ($CoinId == false) {
                return RestResponseFactory::ok(RestUtils::getStdObj(), 30001, 30001, RestUtils::getErrorMessage(30001));;

            }
            $action = $data['type'] == 1 ? UserCoinLog::STOREWALLET : UserCoinLog::STOREMONEY;
            //插入币的流水表
            CoinFactory::addUserCoinLog($user->id, $data['number'], $CoinId, $action,CoinStoreLog::IGNORE);
//            DB::commit();

            $result['storeCoin'] = CoinFactory::getCoinStoreLog($CoinId);
            return RestResponseFactory::ok($result,200);


//        }catch (\Exception $e) {
//
//            DB::rollBack();
//
//            SLogger::getStream()->error('提现失败', [
//                'usr_id' => $user->id,
//            ]);
//            SLogger::getStream()->error($e->getMessage());
//
//        }

    }

    //确认存币
    public function confirmStore(Request $request)
    {
        //存币
        $data = $request->all();
        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        if($data['status'] == 1){
            $resultUpdate = CoinFactory::updateCoinStore($data['id'],$data,$user->id);

            $action = $data['type'] == 1 ? UserCoinLog::STOREWALLET : UserCoinLog::STOREMONEY;
            $resultAdd = CoinFactory::addUserCoinLog($user->id, $data['number'], $data['id'], $action,CoinStoreLog::APPLY);

        if($resultAdd && $resultUpdate)
        {

            return RestResponseFactory::ok(RestUtils::getStdObj(),200);
        }
            return RestResponseFactory::ok(RestUtils::getStdObj(), 30005, 30005, RestUtils::getErrorMessage(30005));;
        }

        CoinFactory::delCoinStore($data['id'],$user->id);
        CoinFactory::delUserCoin($data['id'],$user->id);

        return RestResponseFactory::ok(RestUtils::getStdObj(),200);


    }

    //存币列表
    public function storeList(Request $request)
    {

        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        $data['list'] = CoinFactory::getStoreList($user->id);

        return RestResponseFactory::ok($data,200);


    }

    public function drawCoin(Request $request)
    {
        //存币
        $data = $request->all();
        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        //ToDO:
        //要判断币够不够提
        //提币要手续费多少




        //首先查询是否有未来处理的订单
        $hasCoin = CoinFactory::hasCoinDrawLog($user->id);

        if($hasCoin == true)
        {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 30002, 30002, RestUtils::getErrorMessage(30002));;

        }

//        DB::beginTransaction();
//        try {
        //插入存币日志
        $total_bi = $user->total_bi;
        $CoinId = CoinFactory::addCoinDrawLog($user->id, $data['number'],$total_bi);

        if ($CoinId == false) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 30001, 30001, RestUtils::getErrorMessage(30001));;

        }
        //插入币的流水表
        CoinFactory::addUserCoinLog($user->id, $data['number'], $CoinId,UserCoinLog::STORE,CoinDrawLog::IGNORE);

//            DB::commit();

        $result['drawCoin'] = CoinFactory::getCoinDrawLog($CoinId);
        return RestResponseFactory::ok($result,200);


//        }catch (\Exception $e) {
//
//            DB::rollBack();
//
//            SLogger::getStream()->error('提现失败', [
//                'usr_id' => $user->id,
//            ]);
//            SLogger::getStream()->error($e->getMessage());
//
//        }

    }



    //确认提币
    public function confirmDraw(Request $request)
    {
        //存币
        $data = $request->all();
        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        CoinFactory::updateCoinDraw($data['id'],$status = CoinStoreLog::APPLY);
        CoinFactory::addUserCoinLog($user->id,$data['number']+$data['number'] * CoinDrawLog::FEE, $data['id'], UserCoinLog::DRAW,CoinDrawLog::APPLY);

        return RestResponseFactory::ok(RestUtils::getStdObj(),200);

    }

    //存币列表
    public function drawList(Request $request)
    {

        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        $data['list'] = CoinFactory::getDrawList($user->id);

        return RestResponseFactory::ok($data,200);


    }


    //以太坊兑换ABC
    public function exchange(Request $request)
    {
        //拿到中间件里赋值的userId
        $user = $request -> get('user');
        $data = $request->all();
        //首先判断以太坊足不足
        if($user->total_bi < $data['eth'])
        {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 30004, 30004, RestUtils::getErrorMessage(30004));;

        }

        $user -> total_bi -= $data['eth'];
        $user -> total += $data['abc'];
        $user -> save();
        return RestResponseFactory::ok(RestUtils::getStdObj(),200);


    }


}